// appointment/index.js
Page({
  data: {
    // 当前选中的科室、医生、日期和时间
    currentDepartment: null,
    currentDoctor: null,
    currentDate: null,
    currentTime: null,

    // 科室列表
    departmentList: [
      {
        id: 1,
        name: '不孕不育',
        icon: '/images/home/ks/byby.png'
      },
      {
        id: 2,
        name: '排卵障碍',
        icon: '/images/home/ks/plza.png'
      },
      {
        id: 3,
        name: '阳痿早泄',
        icon: '/images/home/ks/ywzx.png'
      },
      {
        id: 4,
        name: '性功能障碍',
        icon: '/images/home/ks/xgnza.png'
      },
      {
        id: 5,
        name: '生殖感染',
        icon: '/images/home/ks/szgr.png'
      },
      {
        id: 6,
        name: '泌尿感染',
        icon: '/images/home/ks/mngr.png'
      },
      {
        id: 7,
        name: '包皮包茎',
        icon: '/images/home/ks/bpbj.png'
      },
      {
        id: 8,
        name: '前列腺增生',
        icon: '/images/home/ks/qlxzs.png'
      }
    ],

    // 医生列表
    doctorList: [
      {
        id: 1,
        name: '张医生',
        title: '主任医师',
        specialty: '擅长各类不孕不育、排卵障碍等疾病的诊断与治疗',
        avatar: '/images/image.png'
      },
      {
        id: 2,
        name: '李医生',
        title: '副主任医师',
        specialty: '擅长男性泌尿系统疾病、性功能障碍等疾病的诊断与治疗',
        avatar: '/images/image.png'
      },
      {
        id: 3,
        name: '王医生',
        title: '主治医师',
        specialty: '擅长生殖感染、泌尿感染等疾病的诊断与治疗',
        avatar: '/images/image.png'
      }
    ],

    // 日期列表（未来7天）
    dateList: [],

    // 时间段列表
    timeList: [
      { time: '08:00-09:00', status: '可预约' },
      { time: '09:00-10:00', status: '可预约' },
      { time: '10:00-11:00', status: '已满' },
      { time: '11:00-12:00', status: '可预约' },
      { time: '14:00-15:00', status: '可预约' },
      { time: '15:00-16:00', status: '可预约' },
      { time: '16:00-17:00', status: '已满' },
      { time: '17:00-18:00', status: '可预约' }
    ]
  },

  onLoad: function (options) {
    // 如果从首页传递了医生ID，则自动选中该医生
    if (options.doctorId) {
      this.setData({
        currentDoctor: parseInt(options.doctorId)
      });
      
      // 根据医生ID查找对应的科室并选中
      const doctor = this.data.doctorList.find(item => item.id === parseInt(options.doctorId));
      if (doctor) {
        // 这里简单处理，实际应该根据医生信息获取科室ID
        this.setData({
          currentDepartment: 1
        });
      }
    }
    
    // 生成未来7天的日期列表
    this.generateDateList();
  },

  // 生成未来7天的日期列表
  generateDateList: function() {
    const dateList = [];
    const weekdays = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
    
    for (let i = 0; i < 7; i++) {
      const date = new Date();
      date.setDate(date.getDate() + i);
      
      const month = date.getMonth() + 1;
      const day = date.getDate();
      const weekday = weekdays[date.getDay()];
      
      dateList.push({
        date: `${month}月${day}日`,
        day: weekday,
        status: i === 0 ? '今天' : '可约'
      });
    }
    
    this.setData({
      dateList: dateList,
      currentDate: dateList[0].date
    });
  },

  // 选择科室
  selectDepartment: function(e) {
    const departmentId = e.currentTarget.dataset.id;
    this.setData({
      currentDepartment: departmentId
    });
  },

  // 选择医生
  selectDoctor: function(e) {
    const doctorId = e.currentTarget.dataset.id;
    this.setData({
      currentDoctor: doctorId
    });
  },

  // 选择日期
  selectDate: function(e) {
    const date = e.currentTarget.dataset.date;
    this.setData({
      currentDate: date
    });
  },

  // 选择时间段
  selectTime: function(e) {
    const time = e.currentTarget.dataset.time;
    // 检查该时间段是否已满
    const timeItem = this.data.timeList.find(item => item.time === time);
    if (timeItem && timeItem.status !== '已满') {
      this.setData({
        currentTime: time
      });
    } else {
      wx.showToast({
        title: '该时间段已约满',
        icon: 'none'
      });
    }
  },

  // 提交预约
  submitAppointment: function() {
    // 检查是否已选择所有必要信息
    if (!this.data.currentDepartment) {
      wx.showToast({
        title: '请选择科室',
        icon: 'none'
      });
      return;
    }
    
    if (!this.data.currentDoctor) {
      wx.showToast({
        title: '请选择医生',
        icon: 'none'
      });
      return;
    }
    
    if (!this.data.currentDate) {
      wx.showToast({
        title: '请选择日期',
        icon: 'none'
      });
      return;
    }
    
    if (!this.data.currentTime) {
      wx.showToast({
        title: '请选择时间段',
        icon: 'none'
      });
      return;
    }
    
    // 获取选中的科室、医生、日期和时间信息
    const department = this.data.departmentList.find(item => item.id === this.data.currentDepartment);
    const doctor = this.data.doctorList.find(item => item.id === this.data.currentDoctor);
    
    // 提交预约信息
    wx.showLoading({
      title: '提交中...',
    });
    
    // 模拟提交预约信息
    setTimeout(() => {
      wx.hideLoading();
      wx.showModal({
        title: '预约成功',
        content: `您已成功预约${this.data.currentDate} ${this.data.currentTime}的${doctor.name}${doctor.title}，请按时前往就诊。`,
        showCancel: false,
        success: (res) => {
          if (res.confirm) {
            // 跳转到我的预约页面
            wx.switchTab({
              url: '/pages/profile/index'
            });
          }
        }
      });
    }, 1500);
  }
});